package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.RobotUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.dto.RobotContentDTO;
import org.springblade.modules.repair.dto.RobotMessageDTO;
import org.springblade.modules.repair.entity.ElevatorReportConfig;
import org.springblade.modules.repair.entity.MaintainReport;
import org.springblade.modules.repair.entity.MaintainReportSend;
import org.springblade.modules.repair.entity.Robot;
import org.springblade.modules.repair.service.ElevatorReportConfigService;
import org.springblade.modules.repair.service.MaintainReportService;
import org.springblade.modules.repair.service.RobotService;
import org.springblade.modules.system.dto.ReportConfig;
import org.springblade.modules.system.entity.TenantConfig;
import org.springblade.modules.system.service.TenantConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.HashMap;
import java.util.List;

/**
 * @Date 15:26 2024/7/17
 * @Description {
 *   报告
 * }
 * @Author lsay
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/maintain/report")
public class MaintainReportController {

	private final MaintainReportService reportService;
	private final ElevatorReportConfigService elevatorReportConfigService;
	private final TenantConfigService tenantConfigService;

	/**
	 * @Author lsay
	 * @Description {
	 *   根据维修工单数据生成报告
	 * }
	 * @Date 11:23 2024/7/18
	 * @Param 维修工单id
	 * @return 报告详情
	 **/
	@GetMapping("/select/by/wx/plan")
	public R<MaintainReport> selectReportByWxPlan(Long planId){
		return R.data(reportService.selectReportByWxPlan(planId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   新增或更新报告
	 * }
	 * @Date 11:24 2024/7/18
	 * @Param
	 * @return
	 **/
	@PostMapping("/submit")
	public R<?> submit(@RequestBody MaintainReport report){
		reportService.saveOrUpdate(report);
		return R.data(report.getId());
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   获取以生成数据的报告详情
	 * }
	 * @Date 11:24 2024/7/18
	 * @Param 报告id
	 * @return 报告详情
	 **/
	@GetMapping("/detail")
	public R<MaintainReport> detail(Long reportId){
		return R.data(reportService.detail(reportId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   报告发送记录
	 * }
	 * @Date 15:48 2024/7/18
	 * @Param
	 **/
	@PostMapping("/send/log")
	public R<Boolean> sendLog(@RequestBody MaintainReportSend send){
		return R.data(reportService.sendLog(send));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   发送报告机器人消息
	 * }
	 * @Date 9:28 2024/7/23
	 * @Param
	 * @return
	 **/
	@PostMapping("/send/{reportId}")
	public R<Boolean> send(@PathVariable Long reportId){
		return R.data(reportService.send(reportId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据维修工单获取报告统计 （2024-07-23 之前的维修工单不做校验）
	 * }
	 * @Date 11:37 2024/7/23
	 * @Param
	 * @return
	 **/
	@GetMapping("/select/by/wx/count")
	public R<HashMap<String,Integer>> selectReportByWxCount(Long planId){
		return R.data(reportService.selectReportByWxCount(planId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   查询项目电梯发送报告配置
	 * }
	 * @Date 17:10 2024/7/23
	 * @Param
	 * @return
	 **/
	@GetMapping("/select/elevator/config")
	public R<List<ElevatorReportConfig>> selectElevatorReportConfigList(){
		return R.data(elevatorReportConfigService.list(Wrappers.<ElevatorReportConfig>lambdaQuery().eq(ElevatorReportConfig::getTenantId,AuthUtil.getTenantId())));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   查询电梯是否开通报告配置
	 * }
	 * @Date 12:05 2024/7/24
	 * @Param
	 * @return
	 **/
	@GetMapping("/select/elevator/config/result")
	public R<Boolean> selectElevatorReportConfigResult(Long elevatorId){
		TenantConfig tenantConfig = tenantConfigService.getTenantConfig(AuthUtil.getTenantId());
		ReportConfig reportConfig = tenantConfig.getReportConfig();
		if(reportConfig.getWxOpenOrNot().equals(1)){
			return R.data(Boolean.FALSE);
		}else {
			if(reportConfig.getWxNumType().equals(1)){
				return R.data(Boolean.TRUE);
			}else {
				int count = elevatorReportConfigService.count(Wrappers.<ElevatorReportConfig>lambdaQuery().eq(ElevatorReportConfig::getElevatorId,elevatorId));
				if(count > 0){
					return R.data(Boolean.TRUE);
				}else {
					return R.data(Boolean.FALSE);
				}
			}
		}
	}

}
